package com.woorigil.social.domain;

import com.woorigil.social.BaseObject;

public class Condition extends BaseObject {
	protected int itemCountInPage = 45;
	protected int pageCountInPage = 5;

	private int totalItemCount;
	private String query;
	private Order order = Order.HIT;
	private int page;
	
	public static enum Order {
		BUY_COUNT, PRICE_ROW, PRICE_HEIGH, RATE, NEW, HIT
	}
	
	public int getPage() {
		if (page < 1) {
			return 1;
		}
		return page;
	}
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
	
	public int getOffset() {
		return (getPage() - 1) * itemCountInPage;
	}

	public int getLimit() {
		return itemCountInPage;
	}

	public int getPageFrom() {
		return (getPage() - 1) - ((getPage() - 1) % pageCountInPage) + 1; 
	}
	
	public int getPageTo() {
		int maxByRange = getPageFrom() + (pageCountInPage - 1);
		return (maxByRange <= getLastPage()) ? maxByRange : getLastPage();
	}
	
	public int getLastPage() {
		return (totalItemCount % itemCountInPage == 0) ? (totalItemCount / itemCountInPage) : (totalItemCount / itemCountInPage) + 1;
	}	
	
	public void setPage(int page) {
		this.page = page;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public int getTotalItemCount() {
		return totalItemCount;
	}

	public void setTotalItemCount(int totalItemCount) {
		this.totalItemCount = totalItemCount;
	}

	public int getItemCountInPage() {
		return itemCountInPage;
	}

	public void setItemCountInPage(int itemCountInPage) {
		this.itemCountInPage = itemCountInPage;
	}
}